﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Drawing;
using JRCore.protocol;
using System.Threading;

namespace WHYHLWebservices
{
    public static class YHLUtil
    {

        //<?xml version="1.0" encoding="UTF-8"?>
        //<PASSCAR>
        //<PASSTIME>2015-01-27 11:32:56 478</PASSTIME>
        //<CARSTATE>2</CARSTATE>
        //<CARPLATE>A12345</CARPLATE>
        //<PLATETYPE>2</PLATETYPE>
        //<SPEED>80</SPEED>
        //<PLATECOLOR>1</PLATECOLOR>
        //<LOCATIONID>1</LOCATIONID>
        //<DRIVEWAY>1</DRIVEWAY>
        //<DRIVEDIR>1</DRIVEDIR>
        //<CAPTUREDIR>1</CAPTUREDIR>
        //<CARCOLOR>1</CARCOLOR>
        //<CARBRAND>1</CARBRAND>
        //<TGSID>420100000000000149</TGSID>
        //<PLATECOORD>1,1,1,1</PLATECOORD>
        //<CABCOORD>1,1,1,1</CABCOORD>
        //<IMGID1>aW1nMTA=</IMGID1>
        //<IMGID2>aW1nMjA=</IMGID2>
        //<IMGID3/>
        //<CARTYPE>k41</CARTYPE>
        //</PASSCAR>
        public static string MakeXml(PicCenterPush mqparam)
        {
            string ans = "";
            XmlDocument doc = new XmlDocument();
            Dictionary<string, string> kvp = new Dictionary<string, string>();
            XmlElement root;
            try
            {
                root = doc.CreateElement("PASSCAR");
                doc.AppendChild(root);
                CreateNode(doc, "PASSCAR", "PASSTIME", GetPassTime(mqparam.P18), null);//2015-01-27 11:32:56 478
                CreateNode(doc, "PASSCAR", "CARSTATE", "1", null);
                CreateNode(doc, "PASSCAR", "CARPLATE", GetNullPlate(mqparam.P23), null);
                CreateNode(doc, "PASSCAR", "PLATETYPE", GetPlateType(mqparam.P21), null);
                CreateNode(doc, "PASSCAR", "SPEED", mqparam.P27, null);
                CreateNode(doc, "PASSCAR", "PLATECOLOR", GetCarPlateColor(mqparam.P22), null);
                CreateNode(doc, "PASSCAR", "LOCATIONID", mqparam.P8, null);
                CreateNode(doc, "PASSCAR", "DRIVEWAY", mqparam.P17, null);
                CreateNode(doc, "PASSCAR", "DRIVEDIR", GetRoadDirection(mqparam.P16), null);
                CreateNode(doc, "PASSCAR", "CAPTUREDIR", "1", null);
                CreateNode(doc, "PASSCAR", "CARCOLOR", GetCarColor(mqparam.P24), null);
                CreateNode(doc, "PASSCAR", "CARBRAND", GetCarCLPP(mqparam.P45), null);
                CreateNode(doc, "PASSCAR", "TGSID", mqparam.P13, null);
                CreateNode(doc, "PASSCAR", "PLATECOORD", mqparam.P46, null);
                CreateNode(doc, "PASSCAR", "CABCOORD", mqparam.P46, null);
                //TransferForm.PrintSysMsg4Debug(doc.OuterXml);//不加图片进行打印
                CreateNode(doc, "PASSCAR", "IMGID1", GetLocalImageBase64(mqparam.P30), null);
                CreateNode(doc, "PASSCAR", "IMGID2", "", null);
                CreateNode(doc, "PASSCAR", "IMGID3", "", null);
                CreateNode(doc, "PASSCAR", "CARTYPE", GetCarType(mqparam.P44), null);
                ans = doc.OuterXml;
                doc.RemoveAll();
            }
            catch (Exception er)
            {
                
                LogHelper.WriteLog(typeof(YHLUtil), "MakeXml 异常:{0}" + er.Message);

            }
            return ans;
        }

        private static string GetPlateType(string platetype)
        {
            string returnPlateType = "99";
            switch(platetype)
            {
                case "01":
                    returnPlateType = "1";
                    break;
                case "02":
                    returnPlateType = "2";
                    break;
                case "03":
                    returnPlateType = "3";
                    break;
                case "04":
                    returnPlateType = "4";
                    break;
                case "05":
                    returnPlateType = "5";
                    break;
                case "06":
                    returnPlateType = "6";
                    break;
                case "07":
                    returnPlateType = "7";
                    break;
                case "08":
                    returnPlateType = "8";
                    break;
                case "09":
                    returnPlateType = "s9";
                    break;
                default:
                    returnPlateType = "99";
                    break;

            }
            return returnPlateType;
        }

        //获取车辆类型--------------需要扩展--------------------
        public static string GetCarType(string strCarType) 
        {
            string stCarTypeGB = "K33";
            switch (strCarType)//最少五种:小型客车、中型客车、中型货车、大型客车、大型货车
            {
                case "PassengerCar"://客车
                    stCarTypeGB = "K21";
                    break;
                case "LargeTruck"://大货车
                    stCarTypeGB = "H11";
                    break;
                case "MidTruck"://中货车
                    stCarTypeGB = "H21";
                    break;
                case "MicroTruck"://小货车
                    stCarTypeGB = "H31";
                    break;
                case "SaloonCar"://轿车
                    stCarTypeGB = "K33";
                    break;
                case "Microbus"://面包车
                    stCarTypeGB = "K39";
                    break;                
            }
             
            return stCarTypeGB;
        }

        //获取车辆品牌编号
        public static string GetCarCLPP(string clpp)
        {
            string strClppId = "99";
            switch (clpp)
            {
                case "Unknown"://"Unknown"未知 
                    strClppId = "";
                    break;
                case "Audi"://"Audi" 奥迪 3
                    strClppId = "3";
                    break;
                case "Honda"://"Honda" 本田 9
                    strClppId = "9";
                    break;
                case "Buick"://"Buick" 别克 12
                    strClppId = "12";
                    break;
                case "Volkswagen"://"Volkswagen" 大众 21
                    strClppId = "21";
                    break;
                case "Toyota"://"Toyota" 丰田 30
                    strClppId = "30";
                    break;
                case "BMW": //"BMW" 宝马 4
                    strClppId = "4";
                    break;
                case "Peugeot"://"Peugeot" 标致 11
                    strClppId = "11";
                    break;
                case "Ford"://"Ford" 福特 31
                    strClppId = "31";
                    break;
                case "Mazda": //"Mazda" 马自达 64
                    strClppId = "64";
                    break;
                case "Nissan"://"Nissan" 尼桑 74
                    strClppId = "74";
                    break;
                case "Hyundai"://"Hyundai" 现代 89
                    strClppId = "89";
                    break;
                case "Suzuki"://"Suzuki" 铃木 60
                    strClppId = "60";
                    break;
                case "Citroen"://"Citroen" 雪铁龙 91
                    strClppId = "91";
                    break;
                case "Benz"://"Benz" 奔驰 7
                    strClppId = "7";
                    break;
                case "BYD"://"BYD" 比亚迪 10
                    strClppId = "10";
                    break;
                case "Geely"://"Geely" 吉利 42
                    strClppId = "42";
                    break;
                case "Lexus"://"Lexus" 雷克萨斯 54
                    strClppId = "54";
                    break;
                case "Chevrolet"://"Chevrolet" 雪佛兰 90
                    strClppId = "90";
                    break;
                case "Chery"://"Chery" 奇瑞 71
                    strClppId = "71";
                    break;
                case "Kia"://"Kia" 起亚 72
                    strClppId = "72";
                    break;
                case "Charade"://"Charade" 夏利 88
                    strClppId = "88";
                    break;
                case "DF"://"DF" 东风 24
                    strClppId = "24";
                    break;
                case "Naveco"://"Naveco" 依维柯 无对应
                    strClppId = "99";
                    break;
                case "SGMW"://"SGMW" 五菱 87
                    strClppId = "87";
                    break;
                case "Jinbei"://"Jinbei" 金杯 46
                    strClppId = "46";
                    break;
            }

            return strClppId;
        }
                
        //转换车牌坐标格式
        public static string GetCarPlateXY(string strXY) 
        {
            //left，top，right，bottom
            //X1（左上）,y1（左上）,x2（右下）,y2（右下）
            return strXY;

        }
        //获取本地图片并转为BASE64编码格式
        public static String GetLocalImageBase64(string strLocalUrl) 
        {


            String strbaser64 = "";

            if (strLocalUrl == null || strLocalUrl.Length == 0)
            {
                LogHelper.WriteLog(typeof(YHLUtil), "if---" + strbaser64);

                return strbaser64;
            }


            try
            {
                Thread.Sleep(500);

                Bitmap bmp = new Bitmap(strLocalUrl);


                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();

                strbaser64 = Convert.ToBase64String(arr);

            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(typeof(YHLUtil), ex.Message);

            }



            return strbaser64;



            /*try
            {
                Thread.Sleep(1000);
                FileStream fs = new FileStream(strLocalUrl, FileMode.Open);
                //FileStream fs = new FileStream(strLocalUrl, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((int)fs.Length);
                br.Close();
                fs.Close();
                strbaser64 = Convert.ToBase64String(bytes);
                LogHelper.WriteLog(typeof(YHLUtil), "成功--" + strbaser64);                //MemoryStream ms = new MemoryStream(bytes);

                //var byteArray = File.ReadAllBytes(strLocalUrl);



            }
            catch (System.Exception ex)
            {
                 LogHelper.WriteLog(typeof(YHLUtil),ex);

            }

            return strbaser64;*/
        }

        //获取车身颜色
        public static string GetCarColor(string strCarColor) 
        {
            //JARI  0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色
            //JARI  9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
            //1	白
            //2	灰
            //3	黄
            //4	粉
            //5	红
            //6	紫
            //7	绿
            //8	蓝
            //9	棕
            //10黑
            //99其他
            string strBZColor = "99";
            switch (strCarColor)
            {
                case "0":
                    strBZColor = "99";
                    break;
                case "2":
                    strBZColor = "99";
                    break;
                case "3":
                    strBZColor = "2";
                    break;
                case "4":
                    strBZColor = "10";
                    break;
                case "6":
                    strBZColor = "8";
                    break;
                case "7":
                    strBZColor = "8";
                    break;
                case "8":
                    strBZColor = "3";
                    break;
                case "9":
                    strBZColor = "7";
                    break;
                case "10":
                    strBZColor = "9";
                    break;
                case "11":
                    strBZColor = "4";
                    break;
                case "12":
                    strBZColor = "6";
                    break;
            }

            return strBZColor;
        }

        //获取行驶方向
        public static string GetRoadDirection(string strDirect)
        {
            //易华录标准   
            //由北向南	1
            //由西向东	2
            //由南向北	3
            //由东向西	4
            if (strDirect == "1")//原来代表由东向西
            {
                return "4";
            }
            else if (strDirect == "2") //原来代表由南向北
            {
                return "3";
            }
            else if (strDirect == "3") //原来代表由西向东
            {
                return "2";
            }
            else if (strDirect == "4") //原来代表由北向南
            {
                return "1";
            }

            return strDirect;
        }


        //获取车牌颜色
        public static string GetCarPlateColor(string strPlateColor)
        {
            //蓝 黄 白 黑
            //0白;1黄;2蓝;3黑;4其他
            string strNewColor = "4";
            switch (strPlateColor)
            {
                case "蓝":
                    strNewColor = "2";
                    break;
                case "黄":
                    strNewColor = "1";
                    break;
                case "白":
                    strNewColor = "0";
                    break;
                case "黑":
                    strNewColor = "3";
                    break;
            }

            return strNewColor;
        }

        //获取通过时间
        private static string GetPassTime(string strVioTime) 
        {
            //2015-01-27 11:32:56 478
            return string.Format("{0}-{1}-{2} {3}:{4}:{5} {6}", strVioTime.Substring(0, 4),
                    strVioTime.Substring(4, 2), strVioTime.Substring(6, 2), strVioTime.Substring(8, 2),
                    strVioTime.Substring(10, 2), strVioTime.Substring(12, 2), strVioTime.Substring(14,3));
        }

        //获取空车牌信息
        private static string GetNullPlate(string strHPHM) 
        {
            if (strHPHM == "" || strHPHM == "无车牌") 
            {
                return "无牌";
            }

            return strHPHM;
        }

        private static void CreateNode(XmlDocument xmldoc, string parentnodename, string nodename, string innertext, Dictionary<string, string> attributes)
        {
            if ((xmldoc != null) && (parentnodename != null) && (parentnodename != "") && (nodename != null) && (nodename != ""))
            {
                XmlNode parentnode = xmldoc.SelectSingleNode("descendant::" + parentnodename);
                if (parentnode != null)
                {
                    XmlElement xe = xmldoc.CreateElement(nodename);
                    if (innertext != null)
                    {
                        xe.InnerText = innertext;
                    }
                    if (attributes != null)
                    {
                        foreach (KeyValuePair<string, string> pair in attributes)
                        {
                            xe.SetAttribute(pair.Key, pair.Value);
                        }
                    }
                    parentnode.AppendChild(xe);
                }
            }
        }
    }
}
